package org.rsbot.bot;

import java.applet.Applet;
import java.applet.AppletContext;
import java.applet.AppletStub;
import java.applet.AudioClip;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Vector;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.rsbot.util.GlobalConfiguration.Paths;

public class BotStub
  implements AppletStub, AppletContext
{
  private final Map<URL, WeakReference<Image>> IMAGE_CACHE = new HashMap();
  private final Map<String, InputStream> INPUT_CACHE = Collections.synchronizedMap(new HashMap(2));

  private final Logger log = Logger.getLogger(BotStub.class.getName());
  private final Applet applet;
  private final URL codeBase;
  private final URL documentBase;
  private boolean isActive;
  private final Map<String, String> parameters;

  public BotStub(RSLoader paramRSLoader)
  {
    this.applet = paramRSLoader;
    Crawler localCrawler = new Crawler("http://www." + paramRSLoader.getTargetName() + ".com/");
    this.parameters = localCrawler.getParameters();
    String str = localCrawler.getWorldPrefix();
    try {
      this.codeBase = new URL("http://world" + str + "." + paramRSLoader.getTargetName() + ".com");
      this.documentBase = new URL("http://world" + str + "." + paramRSLoader.getTargetName() + ".com/m0");
    } catch (MalformedURLException localMalformedURLException) {
      throw new RuntimeException(localMalformedURLException);
    }
  }

  public void appletResize(int paramInt1, int paramInt2) {
    Dimension localDimension = new Dimension(paramInt1, paramInt2);
    this.applet.setSize(localDimension);
    this.applet.setPreferredSize(localDimension);
  }

  public Applet getApplet(String paramString) {
    String str = (String)this.parameters.get("name");
    if (str == null)
      return null;
    return str.equals(paramString) ? this.applet : null;
  }

  public AppletContext getAppletContext() {
    return this;
  }

  public Enumeration<Applet> getApplets() {
    Vector localVector = new Vector();
    localVector.add(this.applet);
    return localVector.elements();
  }

  public AudioClip getAudioClip(URL paramURL) {
    this.log.info("NOT YET IMPLEMENTED getAudioClip=" + paramURL);
    return null;
  }

  public URL getCodeBase() {
    return this.codeBase;
  }

  public URL getDocumentBase() {
    return this.documentBase;
  }

  public Image getImage(URL paramURL) {
    synchronized (this.IMAGE_CACHE) {
      WeakReference localWeakReference = (WeakReference)this.IMAGE_CACHE.get(paramURL);
      Image localImage;
      if ((localWeakReference == null) || ((localImage = (Image)localWeakReference.get()) == null)) {
        localImage = Toolkit.getDefaultToolkit().createImage(paramURL);
        localWeakReference = new WeakReference(localImage);
        this.IMAGE_CACHE.put(paramURL, localWeakReference);
      }
      return localImage;
    }
  }

  public String getParameter(String paramString) {
    String str = (String)this.parameters.get(paramString);
    if (paramString != null)
      return str;
    return "";
  }

  public InputStream getStream(String paramString) {
    return (InputStream)this.INPUT_CACHE.get(paramString);
  }

  public Iterator<String> getStreamKeys() {
    return Collections.unmodifiableSet(this.INPUT_CACHE.keySet()).iterator();
  }

  public boolean isActive() {
    return this.isActive;
  }

  public void setActive(boolean paramBoolean) {
    this.isActive = paramBoolean;
  }

  public void setStream(String paramString, InputStream paramInputStream) throws IOException {
    this.INPUT_CACHE.put(paramString, paramInputStream);
  }

  public void showDocument(URL paramURL) {
    showDocument(paramURL, "");
  }

  public void showDocument(URL paramURL, String paramString) {
    if (paramURL.toString().contains("outofdate"))
    {
      this.log.severe("RSBot is currently outdated, please wait patiently for a new version.");
      JOptionPane.showMessageDialog(null, "RSBot is currently outdated, please wait patiently for a new version.", "Outdated", 2);
      File localFile = new File(GlobalConfiguration.Paths.getVersionCache());
      if ((localFile.exists()) && (!localFile.delete()))
        this.log.warning("Unable to clear cache.");
    }
    else if (!paramString.equals("tbi")) {
      this.log.info("Attempting to show: " + paramURL.toString() + " [" + paramString + "]");
    }
  }

  public void showStatus(String paramString) {
    this.log.info("Status: " + paramString);
  }
}